package TokenRecognizer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jefoutta
 */
public class AutomataMaker {

    private Integer pointer;
    private String re;
    private Automata result;
    private Thombson thombson;
    private Subset subset;
    private DirectAFD directafd;
    
    public AutomataMaker(String s){
        pointer = 0;
        result = new Automata();
        thombson = new Thombson();
        subset = new Subset();
        re = s;
        directafd = new DirectAFD(re);
    }

    public Automata getThombson(){
        Automata temp = new Automata();
        temp = evalThombson();
        temp.setAcceptation(temp.getLastState());
        temp.setDeterministic(false);
        temp.setSigma(thombson.getSigma());
        return temp;

    }

    public Automata evalDirectAFN(){
       Automata temp = new Automata();
        if(pointer < re.length())
            switch(re.charAt(pointer)){
                case '|':{
                    pointer++;
                    temp = thombson.or(evalThombson(), evalThombson());
                }break;
                case '*':{
                    pointer++;
                    temp = thombson.kleen(evalThombson());
                }break;
                case '.':{
                    pointer++;
                    temp = thombson.concat(evalThombson(), evalThombson());
                }break;
                default:{
                    pointer++;
                    temp = thombson.basic(new Transition(re.charAt(pointer - 1)));
                }
            }
        return temp;
    }

    public Automata evalThombson(){
        Automata temp = new Automata();
        if(pointer < re.length())
            switch(re.charAt(pointer)){
                case '|':{
                    pointer++;
                    temp = thombson.or(evalThombson(), evalThombson());
                }break;
                case '*':{
                    pointer++;
                    temp = thombson.kleen(evalThombson());
                }break;
                case '.':{
                    pointer++;
                    temp = thombson.concat(evalThombson(), evalThombson());
                }break;
                default:{
                    pointer++;
                    temp = thombson.basic(new Transition(re.charAt(pointer - 1)));
                }
            }
        return temp;
    }

    public Automata evalSubset(Automata tautomata){
        Automata temp;
        subset.setAutomata(tautomata);
        temp = subset.subSet();
        temp.setDeterministic(true);
        return temp;
    }

    public Automata getDirectAFD(){
        Automata temp = directafd.getAFD();
        return temp;
    }
}
